define(['jquery', 'bootstrap', 'backend', 'table', 'form','toastr'], function ($, undefined, Backend, Table, Form,toastr) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'cdkey/index' + location.search,
                    add_url: 'cdkey/add',
                    edit_url: 'cdkey/edit',
                    del_url: 'cdkey/del',
                    multi_url: 'cdkey/multi',
                    import_url: 'cdkey/import',
                    table: 'cdkey',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate:false},
                        // {field: 'depot_id', title: __('Depot_id')},
                        {field: 'depot.name', title: __('Depot.name'), operate: 'LIKE'},
                        {field: 'code', title: __('Code'), operate: 'LIKE'},
                        // {field: 'is_start', title: __('Is_start'), searchList: {"0":__('Is_start 0'),"1":__('Is_start 1')}, formatter: Table.api.formatter.normal},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'overduetime', title: __('Overduetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'is_use', title: __('Is_use'), searchList: {"0":__('Is_use 0'),"1":__('Is_use 1')}, formatter: Table.api.formatter.normal},
                        // {field: 'user_id', title: __('User_id')},
                        // {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            // Controller.api.bindevent();
            Form.api.bindevent($("form[role=form]"),function (data,ret){
                console.log(ret)
                if(ret.data.length){
                    let code = ret.data.join('\r\n')
                    Layer.prompt({title: '以下兑换码已存在,无法添加',area: ['350px', '300px'], formType: 2,value:code,btn2: function(){
                            console.log('执行')
                            parent.$("#table").bootstrapTable('refresh',{});
                        Fast.api.close(data);
                    }}, function(value, index){
                        console.log('执行')
                        parent.$("#table").bootstrapTable('refresh',{});
                        Fast.api.close(data);
                    });
                    console.log('执行')
                    $("#table").bootstrapTable('refresh');
                    return false;
                }

            });
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});